﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Santa_sAdventure.View
{
    class SnowParticle
    {
         public static float DELAY_MAX = 7.0f;

        private Vector2 m_position;
        // Speed
        private Vector2 m_velocity;
        private Vector2 m_acceleration;
        private int m_seed;
        private float m_timePased = 0;
        private float m_timeLived = 0;
        private float m_spawnDelay;

        public SnowParticle(int seed)
        {
            this.m_seed = seed;
            InitParticle();
        }

        // If the particke has begin to "move"
        public bool IsMoving()
        {
            return m_timePased > m_spawnDelay;
        }

        internal void Update(float a_elapsedTimeSeconds)
        {
            // Total time elapsed
            m_timePased += a_elapsedTimeSeconds;
            // If particle´is moving, update position
            if (IsMoving())
            {
                // Total time particle has been moving
                m_timeLived += a_elapsedTimeSeconds;
                m_position = m_position + m_velocity * a_elapsedTimeSeconds;
                m_velocity = m_velocity + m_acceleration * a_elapsedTimeSeconds;                
                // If particle has lived his "life", respawn
                if (IsDead())
                {
                    InitParticle();
                }
            }
        }

        // Reset all values
        private void InitParticle()
        {
            Random rand = new Random(m_seed);
            this.m_position = new Vector2(rand.Next(0,21) + (float)rand.NextDouble() *1 - 1 , -1);
            m_velocity = new Vector2(1, 1);
            m_velocity.Normalize();

            m_timePased = 0;
            m_timeLived = 0;

            m_acceleration = new Vector2(0, 1);

            m_spawnDelay = (float)rand.NextDouble() * DELAY_MAX;
        }

        // Dead?
        private bool IsDead()
        {
            return m_position.X > 20 || m_position.Y > 20;
        }

        // Draw
        internal void Draw(SpriteBatch a_spriteBatch, Camera a_camera, Texture2D a_snowTexture)
        {
            Rectangle snowRec = a_camera.GetVisualRectangle(m_position.X, m_position.Y, 0.4f, 0.4f);
            a_spriteBatch.Draw(a_snowTexture, snowRec, Color.White);
        }
    }
}
